package org.wmq.travelagency02.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wmq.travelagency02.model.DTO.HistoryGroupMessageQueryDTO;
import org.wmq.travelagency02.model.DTO.HistoryMessageQueryDTO;
import org.wmq.travelagency02.model.Entity.GroupMessage;
import org.wmq.travelagency02.model.Entity.Result;
import org.wmq.travelagency02.service.GroupMessageService;

@RestController
@RequestMapping("/group-message")
@RequiredArgsConstructor
public class GroupMessageController {
    private final GroupMessageService groupMessageService;

    @GetMapping("/{chatGroupId}")
    public Result<Page<GroupMessage>> getMessages(@PathVariable Long chatGroupId) {
        Page<GroupMessage> page = groupMessageService.getMessages(chatGroupId);
        return Result.ok(page);
    }

    @GetMapping("/history")
    public Result<Page<GroupMessage>> getHistoryMessages(@ModelAttribute HistoryGroupMessageQueryDTO historyGroupMessageQueryDTO) {
        Page<GroupMessage> page = groupMessageService.getHistoryMessages(historyGroupMessageQueryDTO);
        return Result.ok(page);
    }

    @PostMapping
    public Result<Void> sendGroupMessage(@RequestBody GroupMessage groupMessage){
        this.groupMessageService.save(groupMessage);
        return Result.ok();
    }
}
